﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using YArchitech.Controls;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;

namespace YArchitech.Temp
{
	internal partial class PipeModifyForm : System.Windows.Forms.Form
	{
		public PipeModifyForm()
		{
			this.InitializeComponent();
			base.Icon = Resource.YArchitechIcon;
			base.CenterToParent();
			this.ModifyFlow = false;
			this.ModifySpec = false;
		}

		public bool ModifyFlow { get; set; }

		public bool ModifySpec { get; set; }

		public double Flow { get; protected set; }

		public HYPipeSpecification Spec { get; protected set; }

		public PipeModifyForm(HYPipeStraight straight, HYPipeSystemDocument doc) : this()
		{
			this.m_Document = doc;
			this.m_PipeStraight = new HYPipeStraight();
			this.m_PipeStraight = straight;
			this.Spec = this.m_PipeStraight.Section;
			this.SetFlow(this.m_PipeStraight.Flow);
			this.hyTextBoxLength.SetValue(this.m_PipeStraight.Length);
			if (this.m_Document.Specifications != null)
			{
				foreach (int num in this.m_Document.Specifications.GetDNs())
				{
					this.hyComboBoxDN.AddItem(num.ToString());
				}
				this.hyComboBoxDN.Text = this.Spec.DN.ToString();
			}
			this.UpdateResult(false);
		}

		private void SetFlow(double flow)
		{
			this.hyTextBoxFlow.SetValue(flow * 3600.0);
		}

		private double GetFlow()
		{
			return this.hyTextBoxFlow.GetValue(this.m_PipeStraight.Flow * 3600.0) / 3600.0;
		}

		private void UpdateResult(bool cal = true)
		{
			if (this.m_PipeStraight == null || this.m_Document == null)
			{
				return;
			}
			this.hyTextBoxDiameter.SetValue(this.m_PipeStraight.Diameter);
			if (cal)
			{
				this.m_PipeStraight.Cal(this.m_Document.Option);
			}
			this.hyTextBoxVelocity.SetValue(this.m_PipeStraight.Velocity);
			this.hyTextBoxRm.SetValue(this.m_PipeStraight.Rm);
			this.hyTextBoxCoefficient.SetValue(this.m_PipeStraight.Coefficient);
			this.hyTextBoxPl.SetValue(this.m_PipeStraight.FrictionalResistance);
			this.hyTextBoxPd.SetValue(this.m_PipeStraight.ShockResistance);
			this.hyTextBoxP.SetValue(this.m_PipeStraight.Resistance);
		}

		private void hyTextBoxFlow_TextChanged(object sender, EventArgs e)
		{
			this.m_PipeStraight.Flow = this.GetFlow();
			this.UpdateResult(true);
		}

		private void buttonModify_Click(object sender, EventArgs e)
		{
			this.ModifyFlow = this.checkBoxFlow.Checked;
			this.ModifySpec = this.checkBoxDN.Checked;
			this.Flow = this.GetFlow();
			base.DialogResult = DialogResult.OK;
		}

		private void hyComboBoxDN_textChanged(object sender, EventArgs e)
		{
			int value = this.hyComboBoxDN.GetValue(this.m_PipeStraight.DN);
			this.Spec = this.m_Document.Specifications.GetSpecification(value);
			if (this.Spec != null)
			{
				this.m_PipeStraight.Section = this.Spec;
				this.UpdateResult(true);
			}
		}

		private void checkBoxFlow_CheckedChanged(object sender, EventArgs e)
		{
			this.hyTextBoxFlow.Enabled = this.checkBoxFlow.Checked;
		}

		private void checkBoxDN_CheckedChanged(object sender, EventArgs e)
		{
			this.hyComboBoxDN.Enabled = this.checkBoxDN.Checked;
		}

		private HYPipeStraight m_PipeStraight;

		private HYPipeSystemDocument m_Document;
	}
}
